package com.hemerasolutions.v2trainer.model.modality;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.library.Library;

@Entity
public class VersionableModality extends BaseModel {

	private static final long serialVersionUID = 1L;

    @OneToMany(mappedBy="versionableModality",cascade = CascadeType.ALL, fetch=FetchType.LAZY,orphanRemoval=true)
    private List<Modality> modalityVersions;

    @Transient
    private Modality modality;
    
    @ManyToOne
    private Library library;
    
	public VersionableModality() {
		super();
		this.modalityVersions = new ArrayList<Modality>();
		this.addVersion();
		
	}

	public Modality addVersion(){
		Modality modality = new Modality();
		modality.setVersion(1);
		modality.setVersionableModality(this);
		this.modalityVersions.add(modality);		
		

		return modality;
	}
	
	
	public VersionableModality(VersionableModality other, Library library){
		this();

		this.library = library;
		this.modalityVersions.clear();
		
		for(Modality modality: other.modalityVersions){
			
			Modality aux = new Modality(modality,this);
			
			this.modalityVersions.add(aux);

		}
	}
	
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Modality> getModalityVersions() {
		return modalityVersions;
	}

	public void setModalityVersions(List<Modality> modalityVersions) {
		this.modalityVersions = modalityVersions;
	}

	public Modality getCurrentVersion() {
		Modality currentVersion = null;
		int version = 0;
		
		for(Modality modality:this.modalityVersions){
			if(version < modality.getVersion()){
				currentVersion = modality;
				version = currentVersion.getVersion();
			}
		}
		return currentVersion;
	}

	public void setCurrentVersion(Modality currentVersion) {

		for(int x = 0; x < this.getModalityVersions().size(); x++){
			Modality eg = this.getModalityVersions().get(x);
			if(eg.getUuid() ==  currentVersion.getUuid()){
				this.getModalityVersions().set(x,currentVersion);
			}
		} 
	}

	public Library getLibrary() {
		return library;
	}

	public void setLibrary(Library library) {
		this.library = library;
	}

	public Modality getModality() {
		if(this.modality == null){
			this.modality = this.getCurrentVersion();
		}
		for(Modality mod:this.modalityVersions){
			if(mod.getUuid() == this.modality.getUuid()){
				this.modality = mod;
			}
		}
		return this.modality;

	}

	public void setModality(Modality mod) {
		for(int x = 0; x < this.getModalityVersions().size(); x++){
			Modality version = this.getModalityVersions().get(x);
			if(version.getUuid() ==  mod.getUuid()){
				this.getModalityVersions().set(x,mod);
				this.modality = version;
			}
		} 
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((library == null) ? 0 : library.hashCode());
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		VersionableModality other = (VersionableModality) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (library == null) {
			if (other.library != null) {
				return false;
			}
		} else if (!library.equals(other.library)) {
			return false;
		}
		if (uuid == null) {
			if (other.uuid != null) {
				return false;
			}
		} else if (!uuid.equals(other.uuid)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "VersionableModality [id=" + id + ", uuid=" + uuid
				+ ", library=" + library + "]";
	}

}
